package com.openbravo.data.loader;

import com.openbravo.basic.BasicException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class BatchSentence extends BaseSentence
{
  protected Session m_s;
  protected HashMap<String, String> m_parameters;

  public BatchSentence(Session paramSession)
  {
    this.m_s = paramSession;
    this.m_parameters = new HashMap();
  }

  public void putParameter(String paramString1, String paramString2)
  {
    this.m_parameters.put(paramString1, paramString2);
  }

  protected abstract Reader getReader()
    throws BasicException;

  public final void closeExec()
    throws BasicException
  {
  }

  public final DataResultSet moreResults()
    throws BasicException
  {
    return null;
  }

  public DataResultSet openExec(Object paramObject)
    throws BasicException
  {
    BufferedReader localBufferedReader = new BufferedReader(getReader());
    StringBuffer localStringBuffer1 = new StringBuffer();
    ArrayList localArrayList1 = new ArrayList();
    try
    {
      String str;
      while ((str = localBufferedReader.readLine()) != null)
      {
        str = str.trim();
        if ((str.equals("")) || (str.startsWith("--")))
          continue;
        if (str.endsWith(";"))
        {
          localStringBuffer1.append(str.substring(0, str.length() - 1));
          Pattern localPattern = Pattern.compile("\\$(\\w+)\\{([^}]*)\\}");
          Matcher localMatcher = localPattern.matcher(localStringBuffer1.toString());
          ArrayList localArrayList2 = new ArrayList();
          StringBuffer localStringBuffer2 = new StringBuffer();
          Object localObject;
          while (localMatcher.find())
          {
            if ("FILE".equals(localMatcher.group(1)))
            {
              localArrayList2.add(ImageUtils.getBytesFromResource(localMatcher.group(2)));
              localMatcher.appendReplacement(localStringBuffer2, "?");
              continue;
            }
            localObject = (String)this.m_parameters.get(localMatcher.group(1));
            if (localObject == null)
            {
              localMatcher.appendReplacement(localStringBuffer2, Matcher.quoteReplacement(localMatcher.group(0)));
            }
            else
            {
              localArrayList2.add(localObject);
              localMatcher.appendReplacement(localStringBuffer2, "?");
            }
          }
          localMatcher.appendTail(localStringBuffer2);
          try
          {
            if (localArrayList2.size() == 0)
            {
              localObject = new StaticSentence(this.m_s, localStringBuffer2.toString());
              ((BaseSentence)localObject).exec();
            }
            else
            {
              localObject = new PreparedSentence(this.m_s, localStringBuffer2.toString(), SerializerWriteBuilder.INSTANCE);
              ((BaseSentence)localObject).exec(new VarParams(localArrayList2));
            }
          }
          catch (BasicException localBasicException)
          {
            localArrayList1.add(localBasicException);
          }
          localStringBuffer1 = new StringBuffer();
          continue;
        }
        localStringBuffer1.append(str);
      }
      localBufferedReader.close();
    }
    catch (IOException localIOException)
    {
      throw new BasicException(LocalRes.getIntString("exception.noreadfile"), localIOException);
    }
    if (localStringBuffer1.length() > 0)
      localArrayList1.add(new BasicException(LocalRes.getIntString("exception.nofinishedfile")));
    return (DataResultSet)new ExceptionsResultSet(localArrayList1);
  }

  private static class VarParams
    implements SerializableWrite
  {
    private List l;

    public VarParams(List paramList)
    {
      this.l = paramList;
    }

    public void writeValues(DataWrite paramDataWrite)
      throws BasicException
    {
      for (int i = 0; i < this.l.size(); i++)
      {
        Object localObject = this.l.get(i);
        if ((localObject instanceof String))
          paramDataWrite.setString(i + 1, (String)localObject);
        else if ((localObject instanceof byte[]))
          paramDataWrite.setBytes(i + 1, (byte[])(byte[])this.l.get(i));
        else
          paramDataWrite.setObject(i + 1, localObject);
      }
    }
  }

  public class ExceptionsResultSet
    implements DataResultSet
  {
    List l;
    int m_iIndex;

    public ExceptionsResultSet(List arg2)
    {
      Object localObject=arg2;//ati
      this.l = (List) localObject;
      this.m_iIndex = -1;
    }

    public Integer getInt(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public String getString(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public Double getDouble(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public Boolean getBoolean(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public Date getTimestamp(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public byte[] getBytes(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public Object getObject(int paramInt)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public DataField[] getDataField()
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.nodataset"));
    }

    public Object getCurrent()
      throws BasicException
    {
      if ((this.m_iIndex < 0) || (this.m_iIndex >= this.l.size()))
        throw new BasicException(LocalRes.getIntString("exception.outofbounds"));
      return this.l.get(this.m_iIndex);
    }

    public boolean next()
      throws BasicException
    {
      return ++this.m_iIndex < this.l.size();
    }

    public void close()
      throws BasicException
    {
    }

    public int updateCount()
    {
      return 0;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.loader.BatchSentence
 * JD-Core Version:    0.6.0
 */